package com.yssoft.service.production;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import com.yssoft.entity.production.TBatch;
import com.yssoft.mapper.core.TMaterialMapper;
import com.yssoft.mapper.core.TProductsMapper;
import com.yssoft.mapper.production.TBatchMapper;
import com.yssoft.utils.AppUserHelper;

/**
 * @author 戈超
 * @time 2018/5/9 上午 11:02
 */
@Service
public class BatchService {
    private static Logger logger = Logger.getLogger(BatchService.class);

    @Resource
    private TBatchMapper tBatchMapper;
    @Resource
    private TProductsMapper tProductsMapper;
    @Resource
    private TMaterialMapper tMaterialMapper;

    /**
     * 查询生产批次列表
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @param code
     * @param request
     * @return
     * @author 戈超
     */
    public List<Map<String, Object>> queryBatchList(Integer currentPage, Integer pageSize, String userId, String code, HttpServletRequest request) {
        /*查询条件整理*/
        Map<String, Object> param = new HashMap<>();
        /*param.put("userId", userId);*/
        param.put("code", code);
        param.put("tEid", AppUserHelper.getCurrentCompanyId(request));
        param.put("deleteFlag", "0");
        if (currentPage != null && pageSize != null) {
            /*分页整理*/
            int start = (currentPage - 1) * pageSize;
            int end = pageSize;
            param.put("start", start);
            param.put("end", end);
        }
        List<Map<String, Object>> list = tBatchMapper.queryBatchList(param);
        return list;
    }

    /**
     * 选择产品分类查询
     *
     * @param type
     * @param request
     * @return
     * @author 戈超
     */
    public List<Map<String, Object>> queryProductClassifyList(String type, HttpServletRequest request) {
        /*查询条件整理*/
        Map<String, Object> param = new HashMap<>();
        param.put("type", type);
        param.put("tEid", AppUserHelper.getCurrentCompanyId(request));
        param.put("deleteFlag", "0");
        List<Map<String, Object>> list = tBatchMapper.queryProductClassifyList(param);
        return list;
    }

    /**
     * 选择产品查询
     *
     * @param pageSize
     * @param currentPage
     * @param productName
     * @param tPid
     * @param type
     * @param request
     * @return
     * @author 戈超
     */
    public List<Map<String, Object>> queryProduct(Integer currentPage, Integer pageSize, String productName, String type, String tPid, HttpServletRequest request) {
        /*查询条件整理*/
        Map<String, Object> param = new HashMap<>();
        param.put("productName", productName);
        param.put("type", type);
        param.put("tPid", tPid);
        param.put("tEid", AppUserHelper.getCurrentCompanyId(request));
        param.put("deleteFlag", "0");
        if (currentPage != null && pageSize != null) {
            /*分页整理*/
            int start = (currentPage - 1) * pageSize;
            int end = pageSize;
            param.put("start", start);
            param.put("end", end);
        }
        List<Map<String, Object>> list = tBatchMapper.queryProduct(param);
        return list;
    }

    /**
     * 生产批次创建
     *
     * @param tBatch
     * @param request
     * @author 戈超
     */
    public int insertBatch(TBatch tBatch, HttpServletRequest request) {
        /*生产批次数据整理*/
		/*SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = new Date(System.currentTimeMillis());
		tBatch.setCode(formatter.format(date));*/
        tBatch.setUseStat("0");
        tBatch.setDeleteFlag("0");
        tBatch.setCreateTime(new Date());
        tBatch.setCreatorId(AppUserHelper.getCurrentUserId(request));
        tBatch.setCreatorName(AppUserHelper.getCurrentUserName(request));
        tBatch.setDeptId(AppUserHelper.getCurrentDeptId(request));
        tBatch.setDeptName(AppUserHelper.getCurrentDeptName(request));
        tBatch.settEId(AppUserHelper.getCurrentCompanyId(request));
		/*tBatch.setCreatorId(7);
		tBatch.setCreatorName("光头强");
		tBatch.setDeptId(3);
		tBatch.setDeptName("子部门11");
		tBatch.settEId(11);*/
        int result = tBatchMapper.insert(tBatch);
        return result;
    }

    /**
     * 根据生产批次id查询
     *
     * @param id
     * @param request
     * @return
     * @author 戈超
     */
    public Map<String, Object> queryBatchById(Integer id, HttpServletRequest request) {
        /*定义map存储返回数据*/
        Map<String, Object> map = new HashMap<>();
        /*根据id查询生产批次信息*/
        TBatch tBatch = tBatchMapper.selectByPrimaryKey(id);
        /*条件查询参数整理*/
        Map<String, Object> param = new HashMap<>();
        param.put("type", tBatch.getType());
        param.put("tEid", tBatch.gettEId());
        param.put("id", tBatch.gettSourceId());
        param.put("deleteFlag", "0");
        /*查询产品信息*/
        List<Map<String, Object>> productList = tBatchMapper.queryProduct(param);
        if (productList.size() > 0) {
            map = productList.get(0);
        }
        /*整理返回map数据*/
        map.put("pQuantity", tBatch.getpQuantity());
        map.put("startupStat", tBatch.getStartupStat());
        map.put("note", tBatch.getNote());
        map.put("batchCode", tBatch.getCode());
        map.put("sourceId", tBatch.gettSourceId());
        map.put("sourceType", tBatch.getType());

        return map;
    }

    /**
     * 生产批次编辑
     *
     * @param tBatch
     * @return
     * @author 戈超
     */
    public int batchEdit(TBatch tBatch) {
        int result = tBatchMapper.updateByPrimaryKeySelective(tBatch);
        return result;
    }

    /**
     * 生产批次删除
     *
     * @param id
     * @return
     * @author 戈超
     */
    public int batchDelete(Integer id) {
        if (id != null) {
            int result = tBatchMapper.deleteByPrimaryKey(id);
            return result;
        } else {
            return 0;
        }
    }

    /**
     * 生产批次状态修改
     *
     * @param tBatch
     * @return
     * @author 戈超
     */
    public int updateStat(TBatch tBatch) {
        int result = tBatchMapper.updateByPrimaryKeySelective(tBatch);
        return result;
    }

    /**
     * 生产批次选择列表查询
     *
     * @param currentPage
     * @param pageSize
     * @param code
     * @param request
     * @return
     * @author 戈超
     */
    public List<Map<String, Object>> queryBatchSelectList(Integer currentPage, Integer pageSize, String code,
                                                          HttpServletRequest request) {
        /*查询条件整理*/
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);
        param.put("select", "1");
        param.put("tEid", AppUserHelper.getCurrentCompanyId(request));
        param.put("startupStat", 1);
        param.put("deleteFlag", "0");
        if (currentPage != null && pageSize != null) {
            /*分页整理*/
            int start = (currentPage - 1) * pageSize;
            int end = pageSize;
            param.put("start", start);
            param.put("end", end);
        }
        List<Map<String, Object>> list = tBatchMapper.queryBatchList(param);
        /*查询生产批次产品对应物料信息*/
        for (Map<String, Object> map : list) {
            Integer sourceId = Integer.parseInt(map.get("t_source_id").toString());
            if ("1".equals(map.get("type"))) {
                /*成品*/
                List<Map<String, Object>> productsList = tProductsMapper.selectMaterialByProId(sourceId);
                List<Map<String, Object>> productsList2 = tProductsMapper.selectSermigoodByProId(sourceId);
                productsList.addAll(productsList2);
                map.put("materialList", productsList);
            } else if ("2".equals(map.get("type"))) {
                /*半成品*/
                List<Map> semiList = tMaterialMapper.queryYesMaterialById(sourceId);
                map.put("materialList", semiList);
            }
        }
        return list;
    }

    /**
     * 根据id查询生产批次-PC
     *
     * @param id
     * @return
     * @author 王永宁
     */
    public TBatch getOneById(Integer id) {
        return tBatchMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询生产批次列表-成品个体溯源的-标签申请用
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @param code
     * @param request
     * @return
     * @author 王永宁
     */
    public List<Map<String, Object>> queryBatchListForLabel(Integer currentPage, Integer pageSize, String userId, String code, HttpServletRequest request) {
        /*查询条件整理*/
        Map<String, Object> param = new HashMap<>();
        /*param.put("userId", userId);*/
        param.put("code", code);
        param.put("tEid", AppUserHelper.getCurrentCompanyId(request));
        param.put("deleteFlag", "0");
        if (currentPage != null && pageSize != null) {
            /*分页整理*/
            int start = (currentPage - 1) * pageSize;
            int end = pageSize;
            param.put("start", start);
            param.put("end", end);
        }
        List<Map<String, Object>> list = tBatchMapper.queryBatchListForLabel(param);
        return list;
    }

    /**
     * 验证成品或半成品是否关联物料
     *
     * @param id
     * @return
     */
    public boolean isSerGoodMaterial(Integer id) {
        //获取半成品关联物料数量
        int count = tMaterialMapper.getSerGoodMaterialCountByGooId(id);
        //获取成品关联物料数量
        int proCount = tMaterialMapper.getProMaterialCountByGooId(id);
        return count > 0 || proCount > 0;
    }

    /**
     * 查询批次号重复数量
     *
     * @param tBatch
     * @return
     * @author 戈超
     */
    public List<TBatch> queryBatchBySourceId(TBatch tBatch, HttpServletRequest request) {
        if (tBatch.getId() != null && !"".equals(tBatch.getId())) {
            TBatch tBatch1 = tBatchMapper.selectByPrimaryKey(tBatch);
            tBatch.settSourceId(tBatch1.gettSourceId());
            tBatch.setType(tBatch1.getType());
        }
        Map<String, Object> param = new HashMap<>();
        param.put("deleteFlag", "0");
        param.put("tEId", AppUserHelper.getCurrentCompanyId(request));
        param.put("code", tBatch.getCode());
        param.put("sourceId", tBatch.gettSourceId());
        param.put("type", tBatch.getType());
        param.put("batchId", tBatch.getId());
        return tBatchMapper.queryBatchBySourceId(param);
    }

    /**
     * @param batchCode
     * @param sourceId
     * @param sourceType
     * @return List<TBatch>
     * @方法名称: selectBatchByCode
     * @功能描述: 增加校验
     * @作者: 洪丰
     * @创建时间:2018年7月13日 下午2:54:41
     */
    public List<TBatch> selectBatchByCode(String batchCode, Integer sourceId, String sourceType) {
        TBatch bEntity = new TBatch();
        bEntity.setCode(batchCode);
        bEntity.settSourceId(sourceId);
        bEntity.setType(sourceType);
        List<TBatch> bList = tBatchMapper.select(bEntity);
        return bList;
    }
}
